<template>
  <div class="shopcart">
    <div class="content" @click="toggleList">
      <div class="content-left">
        <div class="logo-wrapper">
          <div class="logo" :class="{active: totalCount>0}">
            <span class="icon-shopping_cart"></span>
          </div>
          <span class="count" v-if="totalCount>0">{{totalCount}}</span>
        </div>
        <div class="price" :class="{active: totalCount>0}">&yen;{{totalPrice}}</div>
        <div class="desc" >另需配送费 &yen;{{deliveryPrice}} 元</div>
      </div>
      <div class="content-right">
        <div class="pay" :class="{active:isActive}" @click.stop="pay">
          {{payType}}
        </div>
      </div>
    </div>
    <div class="ball-container">
      <template v-for="ball in balls">
        <transition name="drop" @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
          <div class="ball" v-show="ball.show">
            <div class="inner inner-hook"></div>
          </div>
        </transition>
      </template>
    </div>
    <transition name="fold">
      <div class="shopcart-list" v-if="!fold">
        <div class="list-header">
          <span class="title">已选择</span>
          <span class="empty" @click="emptyList">清空</span>
        </div>
        <div class="list-content" ref="listContent">
          <ul>
            <li class="list-item border-1px"  v-for="food in selectFoods">
              <div class="name">{{food.name}}</div>
              <div class="price">￥{{food.count * food.price}}</div>
              <div class="cartcontrol-wrapper">
                <cartcontrol :food="food"></cartcontrol>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </transition>
    <transition name="fade">
      <div class="list-bg" v-if="!fold" @click="toggleList"></div>
    </transition>
  </div>
</template>

<script type="text/ecmascript-6">
    import cartcontrol from 'components/cartcontrol/cartcontrol.vue'
    import BScroll from 'better-scroll'
    export default {
      data () {
        return {
          isActive: true,
          fold: true,
          balls: [
            {
              show: false
            },
            {
              show: false
            },
            {
              show: false
            },
            {
              show: false
            },
            {
              show: false
            }
          ],
          dropBalls: []
        }
      },
      methods: {
        drop (el) {
          for (let i = 0; i < this.balls.length; i++) {
            let ball = this.balls[i]
            if (!ball.show) {
              ball.show = true
              // 保存el
              ball.el = el
              this.dropBalls.push(ball)
              return
            }
          }
        },
        beforeEnter (el) {
          let count = this.balls.length
          while (count--) {
            let ball = this.balls[count]
            if (ball.show) {
              // getBoundingClientRect 这个方法返回一个矩形对象，包含四个属性：left、top、right和bottom
              let rect = ball.el.getBoundingClientRect()
              let x = rect.left - 32
              let y = -(window.innerHeight - rect.top - 22)
              el.style.display = ''
              el.style.webkitTransform = `translate3d(0, ${y}px, 0)`
              el.style.transform = `translate3d(0, ${y}px, 0)`
              let inner = el.getElementsByClassName('inner-hook')[0]
              inner.style.webkitTransform = `translate3d(${x}px, 0, 0)`
              inner.style.transform = `translate3d(${x}px, 0, 0)`
            }
          }
        },
        enter (el) {
          /* eslint-disable no-unused-vars */
          let rf = el.offsetHeight
          this.$nextTick(() => {
            el.style.webkitTransform = 'translate3d(0, 0, 0)'
            el.style.transform = 'translate3d(0, 0, 0)'
            let inner = el.getElementsByClassName('inner-hook')[0]
            inner.style.webkitTransform = 'translate3d(0, 0, 0)'
            inner.style.transform = 'translate3d(0, 0, 0)'
          })
        },
        afterEnter (el) {
          let ball = this.dropBalls.shift()
          if (ball) {
            ball.show = false
            el.style.display = 'none'
          }
        },
        pay () {
          if (this.totalPrice < this.minPrice) {
            return
          }
          alert(`需支付${this.totalPrice}元`)
        },
        emptyList () {
          this.selectFoods.forEach((food) => {
            food.count = 0
          })
          this.fold = true
        },
        toggleList () {
          if (this.selectFoods.length === 0 && this.fold === true) {
            return
          }
          this.fold = !this.fold
          if (this.fold === false) {
            this.$nextTick(() => {
              this.listScroll = new BScroll(this.$refs.listContent, {
                click: true
              })
            })
          }
        }
      },
      props: {
        selectFoods: {
          type: Array,
          default () {
            return []
          }
        },
        deliveryPrice: {
          type: Number,
          default: 0
        },
        minPrice: {
          type: Number,
          default: 0
        }
      },
      computed: {
        totalPrice () {
          let total = 0
          this.selectFoods.forEach((food) => {
            total += food.price * food.count
          })
          return total
        },
        totalCount () {
          let total = 0
          this.selectFoods.forEach((food) => {
            total += food.count
          })
          if (total === 0) {
            this.fold = true
          }
          if (this.listScroll) {
            console.log(this.listScroll)
            this.$nextTick(() => {
              this.listScroll.refresh()
            })
          }
          return total
        },
        payType () {
          let text = ''
          if (this.totalCount === 0) {
            text = `￥${this.minPrice}起送`
            this.isActive = false
          } else if (this.totalPrice < this.minPrice) {
            text = `还差${this.minPrice - this.totalPrice}起送`
            this.isActive = false
          } else {
            text = `去结算`
            this.isActive = true
          }
          return text
        }
      },
      components: {
        cartcontrol
      }
    }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import '../../common/stylus/mixin.styl'
  .shopcart
    position: fixed
    bottom: 0
    left: 0
    z-index: 50
    width: 100%
    height: 48px
    .content
      display: flex
      display: -webkit-box
      background: #141d27
      .content-left
        flex: 1
        -webkit-box-flex: 1
        font-size: 0
        .logo-wrapper
          display: inline-block
          position: relative
          top: -10px
          margin: 0 12px
          padding: 6px
          width: 56px
          height: 56px
          box-sizing: border-box
          vertical-align: top
          border-radius: 50%
          background: #141d27
          .logo
            &.active
              background: rgb(0,160,220)
              .icon-shopping_cart
                color: #fff
            width: 100%
            height: 100%
            border-radius: 50%
            background: rgba(255,255,255,0.1)
            text-align: center
            .icon-shopping_cart
              font-size: 24px
              line-height: 44px
              color: #80858a
          .count
            padding: 1px 8px
            background: rgb(240,20,20)
            font-size: 12px
            position: absolute
            top: 0px
            right: 0px
            border-radius: 18px
            color: #fff
            font-weight: 700
            box-shadow: 0 2px 4px 0 rgba(0,0,0,0.4)
        .price
          &.active
            color: #fff
          display: inline-block
          vertical-align: top
          margin-top: 12px
          line-height: 24px
          box-sizing: border-box
          padding-right: 12px
          border-right: 1px solid rgba(255,255,255,0.1)
          font-size: 16px
          font-weight: 700
          color: rgba(255,255,255,0.4)
        .desc
          display: inline-block
          margin-top: 12px
          margin-left: 12px
          box-sizing: border-box
          vertical-align: top
          line-height: 24px
          font-size: 10px
          color: rgba(255,255,255,0.4)
      .content-right
        flex: 0 0 105px
        width: 105px
        background: rgba(255,255,255,0.1)
        .pay
          line-height: 48px
          text-align: center
          color: rgba(255,255,255,0.4)
          font-size: 16px
          font-weight: 700
          &.active
            background: #38ca73
            color: #fff


    .ball-container
      .ball
        position: fixed;
        left: 32px;
        bottom: 22px;
        z-index: 200;
        .inner
          width: 16px;
          height: 16px;
          border-radius: 50%;
          background: rgb(0, 160, 220);
        &.drop-enter-active
          transition: all .4s cubic-bezier(.49, -0.29, .75, .41);
          .inner
            transition: all .4s linear;
    .list-bg
      position: fixed
      width: 100%
      height: 100%
      top: 0
      z-index: -2
      backdrop-filter: blur(10px);
      background: rgba(0,0,0,0.7)
      &.fade-enter-active,&.fade-leave-active
        transition: all .5s
      &.fade-enter,&.fade-leave-active
        opacity: 0
    .shopcart-list
      position: absolute
      bottom: 48px
      left: 0
      width: 100%
      z-index: -1
      &.fold-enter-active,&.fold-leave-active
        transition: all .5s
      &.fold-enter,&.fold-leave-active
        opacity: 0
        transform:translate3D(0,100%,0);
        -webkit-transform:translate3D(0,100%,0);
      .list-header
        height: 40px
        line-height: 40px
        padding: 0 18px
        background: #f3f5f7
        border-bottom: 1px solid rgba(7,17,27,0.1)
        .title
          float: left
          font-size: 14px
          color: rgb(7,17,27)
        .empty
          float: right
          font-size: 12px
          color: rgb(0,160,220)
      .list-content
        padding: 0 18px 12px
        max-height: 217px
        overflow: hidden
        background: #fff
        .list-item
          position: relative
          padding: 12px 0
          box-sizing: border-box
          border-1px(rgba(7,17,27,0.1))
          .name
            line-height: 24px
            font-size: 14px
            color: rgb(7,17,27)
          .price
            position: absolute
            right: 100px
            bottom: 12px
            line-height: 24px
            font-size: 14px
            font-weight: 700
            color: rgb(240,20,20)
          .cartcontrol-wrapper
            position: absolute
            top: 6px
            right: 0px
</style>
